package com.sp.ThreadUse;

import java.util.concurrent.*;

/**
 * 线程应用案例3 - 实现Callable接口
 */
public class Thread04 {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(2);
        Callable t1 = new T3();
        Callable t2 = new T3();
        Future task1 = service.submit(t1);
        Future task2 = service.submit(t2);

        try {
            System.out.println(task1.get());
            System.out.println(task2.get());
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        service.shutdown();
    }
}

class T3 implements Callable {

    private int times = 0;
    @Override
    public Object call() throws Exception {
        while(true) {
            System.out.println("喵喵, 我是小猫咪" + (++times) + Thread.currentThread().getName());
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (times == 50) {
                break;
            }
        }
        return true;
    }
}